package netty.buffer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.util.ReferenceCountUtil;
import org.junit.Test;

/**
 * Instruction：
 * Author：@author MaLi
 */
public class C01_Demo {
    @Test
    public void testWriteBuffer() {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(10, 100);
        buffer.writeBytes("Hello Buffer".getBytes());
        System.out.println("写入完成: "+buffer);
        System.out.println("Capacity: "+buffer.capacity());

        //getBuffer
        for (int i = 0; i < buffer.readableBytes(); i++) {
            System.out.println("GetByte: "+buffer.getByte(i));
        }
        //readBuffer
        while (buffer.isReadable()) {
            System.out.println("ReadByte: "+buffer.readByte());
        }
        System.out.println(buffer.refCnt());
        buffer.release();//释放一次引用
        buffer.retain();//增加一次引用
        ReferenceCountUtil.retain(buffer);//增加一次引用
        ReferenceCountUtil.release(buffer);//释放一次引用
    }
}
